package com.zhuizikejithreeig.cn.threeigkeji.loginfo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.zhuizikejithreeig.cn.been.ChengePhoneBeen;
import com.zhuizikejithreeig.cn.been.MesBeen;
import com.zhuizikejithreeig.cn.content.ContectUtils;
import com.zhuizikejithreeig.cn.content.UriContect;
import com.zhuizikejithreeig.cn.threeigkeji.BasicActivity;
import com.zhuizikejithreeig.cn.threeigkeji.CompontApplayCation;
import com.zhuizikejithreeig.cn.threeigkeji.R;
import com.zhuizikejithreeig.cn.utils.GsonUtil;
import com.zhuizikejithreeig.cn.utils.ToastMake;
import com.zhuizikejithreeig.cn.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.MediaType;

public class ChengePhoneActivity extends BasicActivity implements View.OnClickListener {

    private EditText mPhone;
    private Button mButton;
    private EditText mEidt;
    private Button mSubmit;
    private int mTime;
    private TimerTask mWait;
    private Timer timer = new Timer();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView_V(R.layout.activity_chenge_phone);
        setLeftShow(1, R.mipmap.back);
        getLift_V().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        setTitle_V(getResources().getString(R.string.string73));

        mPhone = (EditText) findViewById(R.id.name1);
        mPhone.setText(CompontApplayCation.phone);

        mButton = (Button) findViewById(R.id.name5);
        mSubmit = (Button) findViewById(R.id.name8);
        mEidt = (EditText) findViewById(R.id.name3);

        mButton.setOnClickListener(this);
        mSubmit.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //发送验证码
            case R.id.name5:

                if (isConnected()) {
                    inNumber();
                }
                break;
            //提交
            case R.id.name8:
                if (isConnected()) {
                    exchenged();
                }
                break;
        }

    }


    //修改手机号码
    private void exchenged() {
        final String trim = mPhone.getText().toString().trim();
        String edit = mEidt.getText().toString().trim();
        if (TextUtils.isEmpty(trim)) {
            ToastMake.showToast(getResources().getString(R.string.string51));
            return;
        }
        if (trim.length() != 11) {
            ToastMake.showToast(getResources().getString(R.string.string52));
            return;
        }
        if (!Utils.isMobileNO(trim)) {
            return;
        }
        if (TextUtils.isEmpty(edit)) {
            ToastMake.showToast(getResources().getString(R.string.string54));
            return;
        }
        if (TextUtils.equals(trim, Utils.getString(ContectUtils.PHONESAVE))) {
            ToastMake.showToast(getResources().getString(R.string.string89));
            return;
        }
        showProgrees("修改中,请稍等....");
        Map<String, String> mapA = new HashMap<>();
        mapA.put("phone", Utils.getString(ContectUtils.PHONESAVE));
        mapA.put("newPhone", trim);
        mapA.put("code", edit);
        final String jsonA = GsonUtil.parseMapToJson(mapA);
        new Thread(new Runnable() {
            @Override
            public void run() {
                showProgrees("修改提交中,请稍等...");
                OkHttpUtils
                        .postString()
                        .url(UriContect.CHANGE)
                        .content(jsonA)
                        .mediaType(MediaType.parse("application/json; charset=utf-8"))
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Call call, Exception e, int id) {
                                ToastMake.showToast(getResources().getString(R.string.string58));
                                hideProgree();
                            }

                            @Override
                            public void onResponse(String response, int id) {

                                hideProgree();
                                hideProgree();

                                ChengePhoneBeen been = GsonUtil.parseJsonToBean(response, ChengePhoneBeen.class);
                                if (been == null) {
                                    ToastMake.showToast(getResources().getString(R.string.string58));
                                    return;
                                }
                                if (!TextUtils.equals(been.getError_code(), "0")) {
                                    ToastMake.showToast(been.getMsg());
                                    return;
                                } else {
                                    Utils.setString(trim, ContectUtils.PHONESAVE);
//                                    ToastMake.showToast(been.getMsg());
                                    //修改完成结束
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            finish();
                                        }
                                    });

                                }
                            }
                        });
            }
        }).start();


    }

    private void inNumber() {

        String trim = mPhone.getText().toString().trim();
        if (TextUtils.isEmpty(trim)) {
            ToastMake.showToast(getResources().getString(R.string.string51));
            return;
        }
        if (trim.length() != 11) {
            ToastMake.showToast(getResources().getString(R.string.string52));
            return;
        }
        if (!Utils.isMobileNO(trim)) {

            return;
        }

        mTime = 60;
        mWait = new TimerTask() {
            @Override
            public void run() {
                //再主线程中进行切换
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mTime <= 0) {
                            mWait.cancel();
                            mPhone.setEnabled(true);
                            mPhone.setFocusable(true);
                            mPhone.setFocusableInTouchMode(true);
                            mPhone.requestFocus();
                            mButton.setText(getResources().getString(R.string.string59));
                            mButton.setEnabled(true);
                            ;
                        } else {
                            mPhone.setEnabled(false);
                            mPhone.setFocusable(false);
                            mPhone.setFocusableInTouchMode(false);
                            mButton.setText(mTime + "秒后重获");
                            mButton.setEnabled(false);
                        }
                        mTime--;

                    }
                });

            }
        };

        if (isConnected()) {
            newProject(trim);
            timer.schedule(mWait, 0, 1000);
        } else {
            ToastMake.showToast(getResources().getString(R.string.string57));
            mWait.cancel();
            mPhone.setEnabled(false);
            mPhone.setFocusable(false);
            mPhone.setFocusableInTouchMode(false);
            mPhone.requestFocus();
            mButton.setText(getResources().getString(R.string.string49));
            mButton.setEnabled(false);
            return;
        }

    }

    private void newProject(String phone) {
        HashMap<String, String> map = new HashMap<>();
        map.put("phone", phone);
        map.put("tags", "2");
        String json = GsonUtil.parseMapToJson(map);

        OkHttpUtils
                .postString()
                .url(UriContect.GETCHECKCODE)
                .content(json)
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        mWait.cancel();
                        ToastMake.showToast(getResources().getString(R.string.string58));
                        mPhone.setEnabled(true);
                        mPhone.setFocusable(true);
                        mPhone.setFocusableInTouchMode(true);
                        mPhone.requestFocus();
                        mButton.setText(getResources().getString(R.string.string49));
                        mButton.setEnabled(true);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        MesBeen mesBeen = GsonUtil.parseJsonToBean(response, MesBeen.class);

                        if (mesBeen == null) {
                            mWait.cancel();
                            ToastMake.showToast(getResources().getString(R.string.string58));
                            mPhone.setEnabled(true);
                            mPhone.setFocusable(true);
                            mPhone.setFocusableInTouchMode(true);
                            mPhone.requestFocus();
                            mButton.setText(getResources().getString(R.string.string49));
                            mButton.setEnabled(true);
                            return;
                        }
                        if (!TextUtils.equals(mesBeen.getError_code(), "0")) {
                            ToastMake.showToast(mesBeen.getMsg());
                            mWait.cancel();
                            mPhone.setEnabled(true);
                            mPhone.setFocusable(true);
                            mPhone.setFocusableInTouchMode(true);
                            mPhone.requestFocus();
                            mButton.setText(getResources().getString(R.string.string49));
                            mButton.setEnabled(true);
                            return;
                        }

                    }
                });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (timer!=null) {
            timer = null;
        }
        if (mWait!=null){
            mWait.cancel();
            mWait = null;
        }
    }
}
